package com.he.ymw.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.he.ymw.dao.OrderDao;
import com.he.ymw.pojo.BuyCarVo;
import com.he.ymw.pojo.Order;
import com.he.ymw.pojo.PaymentBO;
import com.he.ymw.service.OrderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderDao orderDao;

    //支付成功后要跳转的页面
    @Value("${alipay.returnUrl}")
    private String returnUrl;


    /**
     * 结算
     * @param cost 所有商品的总价格
     * @param userId 用户id
     * @param proIds 每样商品的id
     * @param quantitys 每样商品的数量
     * @param costs 每样商品的总价格
     */
    @Override
    public int addOrder(Integer cost, Integer userId, String proIds, String quantitys, String costs) {
        // id：数据库自增，orderType:设置为1待付款
        Order order = new Order(0, userId, "", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                cost, new SimpleDateFormat("yyyyMMdd").format(new Date()) + UUID.randomUUID().
                toString().replace("-", ""), 1);
        // 点击结算订单的操作
        if (orderDao.addOrder(order) > 0) {
            System.err.println("添加订单成功");
            // 清空购物车
            if (orderDao.deleteBuyCar(userId) > 0) {
                System.err.println("购物车清空成功");
            }
            String[] prIds = proIds.split(",");
            String[] qs = quantitys.split(",");
            String[] sts = costs.split(",");
            for (int i = 0; i < prIds.length; i++) {
                // 添加订单详情
                orderDao.addOrderDetail(order.getId(),prIds[i],qs[i],sts[i]);
            }
            System.err.println("添加订单详情成功");
            // 将订单id返回
            return order.getId();
        } else {
            return -1;
        }
    }

    /**
     * 查询订单详情
     * @param orderId 订单id
     * @return
     */
    @Override
    public List<BuyCarVo> findOrderDetail(int orderId) {
        System.out.println("查询订单详情");
        return orderDao.findOrderDetail(orderId);
    }

    /**
     * 查询订单号
     * @param orderId 订单id
     */
    @Override
    public String findOrderID(Integer orderId) {
        return orderDao.findOrderID(orderId);
    }

    /**
     * 支付订单
     */
    @Override
    public Object orderPay(PaymentBO paymentBO) {
        try {
            return Factory.Payment.Page().pay(paymentBO.getSubject(), paymentBO.getOrderId(), paymentBO.getTotal().toString(), returnUrl).body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 查询我的订单
     * @param userId 用户id
     * @return
     */
    @Override
    public List<Order> findMyOrder(Integer userId) {
        return orderDao.findMyOrder(userId);
    }

    /**
     * 根据订单id删除订单
     * @param orderId 订单id
     * @return
     */
    @Override
    public boolean deleteOrder(Integer orderId) {
        // 删除订单
        int rs1 = orderDao.deleteOrder(orderId);
        if (rs1 > 0) {
            // 根据订单id删除订单详情
            Integer rs2 = orderDao.deleteOrderDetail(orderId);
            if (rs2 > 0) {
                return true;
            }
        }

        return false;
    }
}
